package RoomController;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import Dao.AdminDao;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class UpdateRoom extends AbstractCommandController{
	
	private AdminDao adminDao;
	
	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}

	@Override
	protected ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response, Object obj, BindException error)
			throws Exception {
	
		//String realFolder = "D:\\Java_workspace\\jHomme\\WebContent\\images";
		 int maxSize = 1024*1024*10;
		 String encType = "UTF-8";
		 String savefile = "images";
		 //ServletContext scontext = getServletContext();
		 String realFolder = getServletContext().getRealPath(savefile);
		 
		 System.out.println("업로드 경로 :" + realFolder );
		 
		 MultipartRequest multi=new MultipartRequest(request, realFolder, maxSize, encType, new DefaultFileRenamePolicy());
		
		 HashMap data = (HashMap)adminDao.admin_detailRoom(Integer.parseInt(multi.getParameter("R_NO")));
		 
		HashMap<String, String> paramlist = new HashMap<>();
		paramlist.put("R_NO",multi.getParameter("R_NO"));
		paramlist.put("R_NAME",multi.getParameter("R_NAME"));
		paramlist.put("R_MIN",multi.getParameter("R_MIN"));
		paramlist.put("R_MAX",multi.getParameter("R_MAX"));
		paramlist.put("R_SIZE",multi.getParameter("R_SIZE"));
		paramlist.put("R_OPTION",multi.getParameter("R_OPTION"));
		if(multi.getFilesystemName("R_THUM")==null){
		String r_thum = data.get("R_THUM").toString();
		paramlist.put("R_THUM", r_thum);
		} else {
		paramlist.put("R_THUM",multi.getFilesystemName("R_THUM"));
		}
		if(multi.getFilesystemName("R_PATH")==null){
		String r_path = data.get("R_PATH").toString();
		paramlist.put("R_PATH", r_path);
		} else {
		paramlist.put("R_PATH",multi.getFilesystemName("R_PATH"));
		}
		
		
		System.out.println(paramlist);
		
		
		adminDao.UpdateRoomta1(paramlist);
		/*adminDao.UpdateRoomta2(paramlist);*/
		System.out.println("방정보 수정완료");
		
		
		List<?> list = adminDao.admin_roomPreview();
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("list",list);
		mv.setViewName("room_admin");
		return mv;		

	}
}

